// pages/orderDetails/orderDetails.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 收货人信息模拟数据
    info:{},
    show:false,
    // 商品信息模拟数据
    array:[],
    actions: [],
    allMoneyzc: 0,
    allMoneyprice: 0,
    overlayShow: false,
    orderMsg: {
      addrId: '',
      currencyAmount: '',
      orderAmount: '',
      productList: [],
      payMethod: ''
    },
    orderId: '',
    pwd: '',
    moreXz: false,
    secondPayPassword: false,
    payFocus: true,
    wxpay: {},
    productList: [],
    nowpay: {}
  },
  // 选择地址关闭
  onClick() {
    this.setData({ show: false });
  },
  toMyAddress(){
    wx.navigateTo({
      url: '/pages/addressSet/addressSet'
    })
  },
  // 选择地址选项
  onSelect(event) {
    this.setData({
      info: event.currentTarget.dataset.name,
      show: false
    })
  },
  // 选择地址打开
  onOpen(){
    this.setData({
      show: true
    })
  },
  // 选择结算方式
  onClickShow(){
    var that = this
    if (!this.data.info) {
      wx.showToast({
        title: '请选择地址！',
        icon: 'none'
      })
      return
    }
    this.setData({
      overlayShow: true
    })
    this.data.orderMsg.addrId = this.data.info.id
    this.data.orderMsg.orderAmount = this.data.allMoneyprice
    this.data.orderMsg.currencyAmount = this.data.allMoneyzc
    this.data.orderMsg.payMethod = 2
    that.data.productList = this.data.nowpay.productList
    this.setData({
      orderMsg: this.data.orderMsg
    })
     // 拿订单号
    let arr = JSON.stringify(that.data.productList)
     let orderAmount = 0
     let currencyAmount = 0
     for (let index = 0; index < that.data.productList.length; index++) {
      orderAmount += that.data.productList[index].amount
      currencyAmount += that.data.productList[index].currency
     }
     wx.request({
      url: app.globalData.baseUrl + '/order/placeOrder',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        addrId: that.data.orderMsg.addrId,
        orderAmount: orderAmount,
        pro: arr,
        currencyAmount: currencyAmount
      },
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        if (res.data.code == 0) {
          that.setData({
            orderId: res.data.data
          })
        }
      }
    })
  },
  // 星钻支付
  xzpay(){
    this.showInputLayer()
    this.setData({
      overlayShow: false
    })
  },
  // 微信支付
  wxpay() {
    var that = this
    // 拿订单号
    that.getwxpayIng()
    this.setData({
      overlayShow: false
    })
  },

  onClose() {
    this.setData({ close: false });
  },
  // 显示密码输入框
  showInputLayer: function(){
    this.setData({ secondPayPassword: true, payFocus: true });
  },
  // 密码输入绑定和完成请求
  inputPwdNewTwo: function(e){
    var that = this
      this.setData({ pwd: e.detail.value });
      if (e.detail.value.length >= 6){
        wx.request({
          url: app.globalData.baseUrl + '/currency/payment',
          data: {
            xcxOpenId: app.globalData.xcxOpenId,
            orderCode: that.data.orderId,
            paymentCode: that.data.pwd,
            type: 'onLine'
          },
          method:'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success:function(res){
            if (res.data.code == 0) {
              that.setData({
                secondPayPassword: false,
                overlayShow: false
              })
              wx.navigateTo({
                url: '../payok/payok'
              })
            }else if(res.data.code == 999995){
              that.setData({
                moreXz: true,
                secondPayPassword: false
              })
            }else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      }
  },
  // 获取焦点
  getFocus: function(){
    this.setData({ payFocus: true });
  },
  // 隐藏输入框
  hidePayLayer: function(){
    this.setData({ secondPayPassword: false, payFocus: false, pwd: '' });
  },
  // 获取微信支付接口
  getwxpayIng(){
    wx.request({
      url: app.globalData.baseUrl + '/onLinePayment',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        orderCode: that.data.orderId,
        orderAmount: that.data.orderMsg.orderAmount
      },
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        if (res.data.code == 0) {
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.package,
            signType: res.data.data.signType,
            paySign: res.data.data.paySign,
            success (res) {
              wx.switchTab({
                url: '../shoppingCart/shoppingCart'
              })
            },
            fail (res) {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
              setTimeout(function (params) {
                wx.switchTab({
                  url: '../shoppingCart/shoppingCart'
                })
              }, 1000)
            }
          })
        }
      }
    })
  },
  // 购买星钻
  buyxz(){
    wx.navigateTo({
      url: '../xingzuan/xingzuan',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    wx.getStorage({
      key: 'buyStr',
      success: function(res){
        let str = res.data
        that.setData({
          nowpay: JSON.parse(str)
        })
      }
    })
    wx.getStorage({
      key: 'allMoneyzc',
      success(res) {
        that.setData({
          allMoneyzc: res.data
        })
      }
    })
    wx.getStorage({
      key: 'allMoneyprice',
      success(res) {
        that.setData({
          allMoneyprice: res.data
        })
      }
    })
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressListByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        pageNum: 1,
        pageSize: 10
      },
      method:'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '您暂未设置地址，请先添加地址。',
            icon: 'none'
          })
          // setTimeout(function () {
          //   wx.navigateTo({
          //     url: '../addressSet/addressSet'
          //   })
          // }, 2000)
          // return
        }
        that.setData({
          actions: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      method:'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        that.setData({
          info: res.data.data
        })
      }
    })
  },
  onShow(){
    wx.request({
      url: app.globalData.baseUrl + '/user/getUserByOpenId',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          if (!res.data.data.paymentCode) {
            wx.showToast({
              title: '您暂未设置密码，请先设置密码。',
              icon: 'none'
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '../me/me'
              })
            }, 2000)
          }
        }
      }
    })
  }
})